#! /usr/bin/python3
# -*- coding: UTF-8 -*-
"""
  作者: 小肥爬爬
  简书: https://www.jianshu.com/u/db796a501972
  gitee: https://gitee.com/xiaofeipapa/python-toolkit
  您可以自由转载此博客文章, 恳请保留原链接, 谢谢!
"""
from fabric import Connection
import fab_utils
from invoke import Responder


def do_it():

    host = '192.168.0.12'
    user = 'root'

    # ssh 连接的正确姿势
    conn = Connection(host=host, user=user)

    # 运行shell之前, 要随便运行一个命令, 获得运行环境
    fab_utils.run(conn, 'uname -a')

    # 将密码改成bb
    username = 'xiaofeipapa'
    pwd = 'bb'
    resp1 = Responder(
        pattern=r'Enter new UNIX password:',
        response=pwd + '\n'
    )

    resp2 = Responder(
        pattern=r'Retype new UNIX password:',
        response=pwd + '\n'
    )

    conn.run('passwd ' + username, pty=True, hide=True, watchers=[resp1, resp2])


if __name__ == '__main__':
    do_it()

